package com.zito.health.service.impl;

import com.zito.health.entity.Extension;
import com.zito.health.entity.ExtensionTrain;
import com.zito.health.mapper.ExtensionMapper;
import com.zito.health.mapper.ExtensionTrainMapper;
import com.zito.health.service.ExtensionTrainService;
import com.zito.health.vo.ExtensionTrainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Sam on 2020/03/12.
 */
@Service
public class ExtensionTrainServiceImpl implements ExtensionTrainService {

    @Autowired
    private ExtensionTrainMapper extensionTrainMapper;
    @Autowired
    private ExtensionMapper extensionMapper;

    @Override
    @Transactional
    public ExtensionTrain save(ExtensionTrain object) {
        extensionTrainMapper.insertSelective(object);
        Extension extension = extensionMapper.selectByPrimaryKey(object.getExtensionId());
        if (extension != null) {
            String periods = object.getPeriods();
            int schedules = 0;
            if ("第一期".equals(periods)) {
                schedules = 1;
            }
            if ("第二期".equals(periods)) {
                schedules = 2;
            }
            if ("第三期".equals(periods)) {
                schedules = 3;
            }
            if ("第四期".equals(periods)) {
                schedules = 4;
            }
            if ("第五期".equals(periods)) {
                schedules = 5;
            }
            if ("第六期".equals(periods)) {
                schedules = 6;
            }
            if ("第七期".equals(periods)) {
                schedules = 7;
            }
            if ("第八期".equals(periods)) {
                schedules = 8;
            }
            if ("第九期".equals(periods)) {
                schedules = 9;
            }
            if ("第十期".equals(periods)) {
                schedules = 10;
            }
            extension.setSchedules(schedules + "");
            extensionMapper.updateByPrimaryKeySelective(extension);
        }
        return object;
    }

    @Override
    public void delete(Integer id) {
        extensionTrainMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ExtensionTrain object) {
        extensionTrainMapper.updateByPrimaryKeySelective(object);
        Extension extension = extensionMapper.selectByPrimaryKey(object.getExtensionId());
        if (extension != null) {
            String periods = object.getPeriods();
            int schedules = 0;
            if ("第一期".equals(periods)) {
                schedules = 1;
            }
            if ("第二期".equals(periods)) {
                schedules = 2;
            }
            if ("第三期".equals(periods)) {
                schedules = 3;
            }
            if ("第四期".equals(periods)) {
                schedules = 4;
            }
            if ("第五期".equals(periods)) {
                schedules = 5;
            }
            if ("第六期".equals(periods)) {
                schedules = 6;
            }
            if ("第七期".equals(periods)) {
                schedules = 7;
            }
            if ("第八期".equals(periods)) {
                schedules = 8;
            }
            if ("第九期".equals(periods)) {
                schedules = 9;
            }
            if ("第十期".equals(periods)) {
                schedules = 10;
            }
            extension.setSchedules(schedules + "");
            extensionMapper.updateByPrimaryKeySelective(extension);
        }
    }

    @Override
    public List<ExtensionTrain> query() {
        return null;
    }

    @Override
    public List<ExtensionTrain> query(ExtensionTrain object) {
        return extensionTrainMapper.select(object);
    }

    @Override
    public ExtensionTrain queryOne(ExtensionTrain object) {
        return null;
    }

    @Override
    public ExtensionTrain queryById(Integer id) {
        return null;
    }

    @Override
    public boolean exists(Integer id) {
        return false;
    }

    @Override
    public List<ExtensionTrainVo> queryList(ExtensionTrainVo param) {
        return extensionTrainMapper.queryList(param);
    }

    @Override
    public ExtensionTrainVo selectById(Integer id) {
        return extensionTrainMapper.selectById(id);
    }
}
